<?php
namespace app\common\lib;

use think\Db;

class SendIntegral{
    /**
     * @param $Info  公告.任务.积分奖扣.请假 表数据
     * @param $eventId 事件id
     * @param int $record_b b分值
     * @param int $record_a a分值
     * @param string $userId 用户id
     */
    //发放积分
    static public function send($Info, $eventId, $record_b = 0, $record_a = 0, $userId = ''){
        switch ($eventId){
            //非系统事件
            case 1:     //奖扣事件参与人得分
                //获取该事件的所有参与人
                $c_i_e_r_u_Infos = M('com_integral_event_relation_users')
                    ->where('com_id', $Info['com_id'])
                    ->where('event_id', $Info['id'])
                    ->select();
                foreach ($c_i_e_r_u_Infos as $k=>$v){
                    $Info['user_id'] = $v['user_id'];
                    self::add_record_integral($Info, $v['integral_b'], $v['integral_a'], $Info['id'], false);
                }
                break;
            //以下是系统事件
            case 17:     //发放任务大厅记录人奖分
                $record_b = M('com_config')->where('com_id', $Info['com_id'])->value('record');
                $Info['user_id'] = $Info['record_user_id'];
                $status = true;
                break;
            case 18:     //
                break;
            case 19:     //
                break;
            case 20:     //
                break;
            case 21:     //
                break;
            case 22:     //
                break;
            case 23:     //发放积分事件，记录人，人次奖励
                break;
            case 24:     //
                break;
            case 25:     //发放积分奖扣记录人奖分
            case 26:     //发放积分奖扣初审人奖分
                //不需要做任何操作，判断的是 $record
                $status = true;
                break;
            case 27:     //
                break;
            case 28:     //病假
            case 29:     //事假
            case 30:     //其他请假
            //请假扣分
                $status = true;
                break;
            case 31:     //固定扣分
                $status = true;
                break;
            case 32:     //固定奖分
                break;
            default:
                $record = 0;
                break;
        }
        if($eventId  > 16 && $eventId < 33) {  //系统事件
            $status = $status ?? false;
            if($record) self::add_record_integral($Info, $record_b, $record_a, $eventId, $status);
        }
    }

    //发放奖分
    private static function add_record_integral($Info, $record_b, $record_a, $eventId, $status)
    {
//        com_integral_prize_buckle
        $time = time();
        //系统奖励
        if($status){
            if(!in_array($eventId, [23,24,25,26])){       //任务大厅，终审给记录人加分。需要模拟一个主题
                $arr = SystemEvent::names();
                $c_i_p_b = [
                    'com_id'=>$Info['com_id'],
                    'first_check_user_id'=>2,
                    'last_check_user_id'=>2,
                    'record_user_id'=>2,
                    'prize_buckle_time'=>$time,
                    'status'=>2,
                    'integral_b_add'=>$record_b,
                    'integral_a_add'=>$record_a,
                    'first_check_time'=>$time,
                    'last_check_time'=>$time,
                    'theme'=>date('Y-m-d', $time).'-'.$Info['name'].'-'.$arr[$eventId] ,
                    'create_time'=>$time,
                ];
                $c_i_p_b_ID = M('com_integral_prize_buckle')->insertGetId($c_i_p_b);
            }else{
                $c_i_p_b_ID = $Info['id'];
            }


            //com_integral_prize_buckle_relation_event
            $c_i_p_b_r_e = [
                'com_id'=>$Info['com_id'],
                'prize_buckle_id'=>$c_i_p_b_ID,
                'event_id' =>$eventId,
                'create_time'=>$time
            ];
            $c_i_p_b_r_e_ID = M('com_integral_prize_buckle_relation_event')->insertGetId($c_i_p_b_r_e);

            //com_integral_event_relation_users
            $c_i_e_r_u = [
                'com_id'=>$Info['com_id'],
                'event_id'=>$c_i_p_b_r_e_ID,
                'user_id'=>$Info['user_id'],
                'integral_b'=>$record_b,
                'integral_a'=>$record_a
            ];
            M('com_integral_event_relation_users')->insert($c_i_e_r_u);

        }else{
            //非系统事件，需要获取   $c_i_p_b_r_e_ID
            //com_integral_prize_buckle_relation_event
            //非系统事件要获取主题id
            $c_i_p_b_r_e_where = [
                'com_id'=>$Info['com_id'],
                'prize_buckle_id'=>$Info['id'],
                'event_id' =>$eventId,
            ];
            $c_i_p_b_r_e_ID = M('com_integral_prize_buckle_relation_event')->where($c_i_p_b_r_e_where)->value('id');
        }
        //修改用户积分
//        users
        $user_id = $Info['user_id'];
        if($record_a) $usersUpdate['integral_a'] = Db::raw('integral_a+'.$record_a);
        if($record_b) $usersUpdate['integral_b'] = Db::raw('integral_b+'.$record_b);
        M('users')->where( 'id', $user_id)->update($usersUpdate);

//        com_integral_log
        //添加用户积分记录
        $c_i_l = [
            'com_id'=>$Info['com_id'],
            'user_id'=>$user_id,
            'type' => 0,
            'integral_b'=> $record_b,
            'integral_a'=> $record_a,
            'create_time'=>$time,
            'year'=>date('Y',$time),
            'month'=>date('m',$time),
            'year_month'=>$time,
            'event_id'=>$c_i_p_b_r_e_ID,
        ];
        M('com_integral_log')->insert($c_i_l);
    }

}
